﻿<!-- 正文开始 -->
<div class="layui-fluid" style="height: calc(100% - 30px);">
    <div class="layui-row layui-col-space15" style="height: 100%;">
        <!-- 左树 -->
        <div class="layui-col-sm12 layui-col-md4 layui-col-lg3" style="height: 100%;">
            <div class="layui-card" style="height: 100%;">
            	<div class="layui-card-header">分类</div>
                <div class="layui-card-body mini-bar" id="treeTbTree" style="height: calc(100% - 150px)">
                    <div class="layui-form toolbar">
                        <div class="layui-form-item">
                            <div class="layui-inline">
                                <div class="layui-input-inline mr0">
                                    <input name="keyword" class="layui-input" type="text" placeholder="输入关键字"/>
                                </div>
                            </div>
                            <div class="layui-inline">
                                <button class="layui-btn icon-btn" lay-filter="form-search-point-classify-btn" lay-submit>
                                    <i class="layui-icon">&#xe615;</i>搜索
                                </button>
                                <button class="layui-btn icon-btn" id="add-point-classify-btn">
                                    <i class="layui-icon">&#xe654;</i>添加
                                </button>                                
                            </div>
                        </div>
                    </div>                
                	<table class="layui-table" id="table-src-point-classify" lay-filter="table-src-point-classify"></table>
                </div>
            </div>
        </div>
        <!-- 右表 -->
        <div class="layui-col-sm12 layui-col-md8 layui-col-lg9">
            <div class="layui-card">
            	<div class="layui-card-header">模型列表</div>
                <div class="layui-card-body" >
                    <div class="layui-form toolbar">
                        <div class="layui-form-item">
                            <div class="layui-inline">
                                <div class="layui-input-inline mr0">
                                    <input name="keyword" class="layui-input" type="text" placeholder="输入关键字"/>
                                </div>
                            </div>
                            <div class="layui-inline">
                                <button class="layui-btn icon-btn" lay-filter="form-point-search-btn" lay-submit>
                                    <i class="layui-icon">&#xe615;</i>搜索
                                </button>
                                <button class="layui-btn icon-btn" id="upload-point-btn">
                                    <i class="layui-icon">&#xe681;</i>上传
                                </button>
                                <!-- <button class="layui-btn icon-btn" id="add-point-btn">
                                    <i class="layui-icon">&#xe654;</i>添加
                                </button> -->                                
                            </div>
                        </div>
                    </div>
                    <table class="layui-table" id="tablePoint" lay-filter="tablePoint"></table>
                </div>
            </div>
        </div>

    </div>
</div>



<!-- 表格操作列 -->
<script type="text/html" id="tableBarPoint">
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>

<!-- 表格操作列 -->
<script type="text/html" id="tableBarPointClassify">
#[[
  {{#  if(d.id === '1' ){ }}
	<span>系统保留</span>
  {{#  } else { }}
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">修改</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
  {{#  } }}
]]#
</script>

<!-- 表单弹窗 -->
<script type="text/html" id="point-classify-form-script">
    <form id="point-classify-form" lay-filter="point-classify-form" class="layui-form model-form">
        <input name="id" type="hidden"/>
        <div class="layui-form-item">
            <label class="layui-form-label">名称</label>
            <div class="layui-input-block">
                <input name="name" placeholder="请输入名称" type="text" class="layui-input" maxlength="50" lay-verType="tips" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">标识</label>
            <div class="layui-input-block">
                <input name="code" placeholder="请输入标识" type="text" class="layui-input" />
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">序号</label>
            <div class="layui-input-block">
                <input name="sort" placeholder="请输入序号" type="number" class="layui-input" />
            </div>
        </div>
        <div class="layui-form-item text-right">
            <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">取消</button>
            <button class="layui-btn" lay-filter="submit-point-classify-filter" lay-submit>保存</button>
        </div>
    </form>
</script>

<!-- 表单弹窗 -->
<script type="text/html" id="point-form-script">
    <form id="pointFormFilter" lay-filter="pointFormFilter" class="layui-form model-form">
        <input name="id" type="hidden"/>
        <div class="layui-form-item" style="text-align: center;">
			<img id="serverMap-thumbnail-img" src="#(ctx)/assets/img/thum-default.jpg" style="width:96px;height:96px;">
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">名称</label>
            <div class="layui-input-block">
                <input name="name" placeholder="请输入名称" type="text" class="layui-input" maxlength="50" lay-verType="tips" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">url</label>
            <div class="layui-input-block">
                <input name="url" placeholder="请输入url" type="text" class="layui-input"  lay-verType="tips" lay-verify="required" required/>
            </div>
        </div>
		<div class="layui-form-item">
		    <label class="layui-form-label">格式</label>
		    <div class="layui-input-block">
		      <select name="type" lay-filter="type">
		        <option value="">--请选择--</option>
				<option value="1">tileset</option>
		        <option value="2">gltf/glb</option>
		      </select>
		    </div>
		</div>
        <div class="layui-form-item text-right">
            <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">取消</button>
            <button class="layui-btn" lay-filter="pointSubmitFilter" lay-submit>保存</button>
        </div>
    </form>
</script>

<!-- 表单弹窗 -->
<script type="text/html" id="pointServerMapData">
    <div class="layui-card">
        <div class="layui-card-body">
            <table class="layui-table" id="tablePointData" lay-filter="tablePointData"></table>
        </div>
    </div>
</script>


<!-- js部分 -->
<script>
layui.use(['layer', 'form', 'table', 'util', 'admin', 'zTree','treetable', 'config','upload'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var util = layui.util;
        var admin = layui.admin;
        var config = layui.config;
        var treetable = layui.treetable;
        var config = layui.config;
        var upload = layui.upload;
        var classifyId='';
        
        // 渲染表格
        var classifyTb = table.render({
            elem: '#table-src-point-classify',
            url: '#(ctx)/resources/point/listDataForClassify',
            page: true,
            cellMinWidth: 100,
            height: 'full-290',
            cols: [[
                {type:'radio'},
                {field: 'name', sort: false, title: '名称'},
                {align: 'center', toolbar: '#tableBarPointClassify', title: '操作'}
            ]]
        });
        
        // 搜索
        form.on('submit(form-search-point-classify-btn)', function (data) {
        	classifyTb.reload({where: data.field}, 'data');
        });

        // 添加
        $('#add-point-classify-btn').click(function () {
        	showClassifyEditModel();
        });
        
        table.on('row(table-src-point-classify)', function(obj){
		  	classifyId = obj.data.id;
		  	insTb.reload({where: {classifyId:obj.data.id}}, 'data');
		});
        
        table.on('tool(table-src-point-classify)', function (obj) {
            var data = obj.data;
            console.log(data);
            var layEvent = obj.event;
            if (layEvent === 'edit') { // 修改
            	showClassifyEditModel(data);
            } else if (layEvent === 'del') { // 删除
                doDelClassify(obj);
            }
            
        });
        
        // 渲染表格
        var insTb = table.render({
            elem: '#tablePoint',
            url: '#(ctx)/resources/point/listData',
            page: true,
            cellMinWidth: 100,
            height: 'full-280',
            cols: [[
                {type: 'numbers'},
                {field: 'thumbnail', sort: false, title: '缩略图'},
                {field: 'name', sort: false, title: '名称'},
                {field: 'x', sort: false, title: '经度'},
                {field: 'y', sort: false, title: '纬度'},
                {field: 'z', sort: false, title: '高度'},
                {align: 'center', toolbar: '#tableBarPoint', title: '操作', minWidth: 150}
            ]]
        });
        
        // 搜索
        form.on('submit(form-point-search-btn)', function (data) {
            insTb.reload({where: data.field}, 'data');
        });

        // 工具条点击事件
        table.on('tool(tablePoint)', function (obj) {
            var data = obj.data;
            var layEvent = obj.event;
            if (layEvent === 'edit') { // 修改
            	 showEditModel(data);
            } else if (layEvent === 'del') { // 删除
                doDel(obj);
            }
            
        });
        
    	upload.render({
    		elem: '#upload-point-btn',
    		url:'#(ctx)/resources/point/upload',
    		accept: 'file',
    		size: 5 * 1024,
    		exts: 'xlsx|xls',
    		data: {
    			classifyId: function() {
    				return classifyId;
    			}
    		},
    		done: function(res) {
    			if (200 == res.code) {
    				layer.msg(res ? res.message : '异常！');
    				insTb.reload({where:{classifyId:classifyId}}, 'data');
    			} else {
    				layer.msg(res ? res.message : '异常！');
    			}
    		},
    		error: function() {
    		}
    	});
        
        // 删除
        function doDel(obj) {
            layer.confirm('确定要删除“' + obj.data.name + '”吗？', {
                skin: 'layui-layer-admin',
                shade: .1
            }, function (i) {
                layer.close(i);
                layer.load(2);
                admin.req('resources/point/delete', {
                    id: obj.data.id
                }, function (res) {
                    layer.closeAll('loading');
                    if (res.code == 200) {
                        layer.msg(res.message, {icon: 1});
                        obj.del();
                    } else {
                        layer.msg(res.message, {icon: 2});
                    }
                }, 'get');
            });
        }

        function doDelClassify(obj) {
            layer.confirm('确定要删除“' + obj.data.name + '”类型吗？', {
                skin: 'layui-layer-admin',
                shade: .1
            }, function (i) {
                layer.close(i);
                layer.load(2);
                admin.req('resources/point/deleteClassify', {
                    id: obj.data.id
                }, function (res) {
                    layer.closeAll('loading');
                    if (res.code == 200) {
                        layer.msg(res.message, {icon: 1});
                        obj.del();
                    } else {
                        layer.msg(res.message, {icon: 2});
                    }
                }, 'get');
            });
        }
        
        // 显示编辑弹窗
        function showClassifyEditModel(mServerMap) {
            admin.open({
                type: 1,
                offset: 't',
                title: (mServerMap ? '修改' : '添加') + '服务',
                content: $('#point-classify-form-script').html(),
                success: function (layero, dIndex) {
                    var url = 'resources/point/saveClassify';
                    form.val('point-classify-form', mServerMap);  // 回显数据
                    // 表单提交事件
                    form.on('submit(submit-point-classify-filter)', function (data) {
                        layer.load(2);
                        var formData={}; 
                        for(var key in data.field){
                        	formData['pointClassify.'+key]=data.field[key];
                        }
                        admin.req(url, formData, function (res) {
                            layer.closeAll('loading');
                            if (res.code == 200) {
                                layer.close(dIndex);
                                layer.msg(res.message, {icon: 1});
                                classifyTb.reload({}, 'data');
                            } else {
                                layer.msg(res.message, {icon: 2});
                            }
                        }, 'get');
                        return false;
                    });
                }
            });
        }
        
        function showEditModel(mServerMap) {
            admin.open({
                type: 1,
                offset: 't',
                title: (mServerMap ? '修改' : '添加') ,
                content: $('#point-form-script').html(),
                success: function (layero, dIndex) {
                    var url = 'resources/point/save';
                    form.val('pointFormFilter', mServerMap);  // 回显数据
    				if (mServerMap && mServerMap.thumbnail) {
    					$('#serverMap-thumbnail-img').attr("src", "#(ctx)" + mServerMap.thumbnail);
    				}
                    if(mServerMap){
        				upload.render({
        					elem: '#serverMap-thumbnail-img'
        					, url: '#(ctx)/resources/point/uploadThumbnail?id=' + mServerMap.id
        					, accept: 'images'
        					, before: function(obj) {
        						layer.load();
        					}
        					, done: function(res, index, upload) {
        						layer.closeAll('loading'); //关闭loading
        						if (200 == res.code) {
        							layer.msg(res.message, { icon: 1 });
        	    					$('#serverMap-thumbnail-img').attr("src", "#(ctx)" + res.data.thumbnail);
        						} else {
        							layer.msg(res.message, { icon: 2 });
        						}
        					}
        					, error: function(index, upload) {
        						layer.closeAll('loading');
        						layer.msg("上传失败！", { icon: 2 });
        					}
        				});
                    }
                    
                    // 表单提交事件
                    form.on('submit(pointSubmitFilter)', function (data) {
                        layer.load(2);
                        var formData={}; 
                        for(var key in data.field){
                        if ('file' !== key)
                       		formData['point.'+key]=data.field[key];
                        }
                        admin.req(url, formData, function (res) {
                            layer.closeAll('loading');
                            if (res.code == 200) {
                                layer.close(dIndex);
                                layer.msg(res.message, {icon: 1});
                                insTb.reload({}, 'data');
                            } else {
                                layer.msg(res.message, {icon: 2});
                            }
                        }, 'get');
                        return false;
                    });
                }
            });
        }        
        
        
    });
</script>